﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Sylvain.Manager;
using Sylvain.Model;
using System.IO;


public partial class Admin_EditContracts : System.Web.UI.Page
{
    ISylvainManager objManger = new SylvainManager();
   
    //static int PageId;
    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    Response.Buffer = true;
    //    Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
    //    Response.Expires = 0;
    //    Response.CacheControl = "no-cache";
    //    if (!Page.User.Identity.IsAuthenticated)
    //        Response.Redirect("~/Admin/AdminLogin.aspx", false);

    //    if (!IsPostBack)
    //    {


    //        PageId = objManger.GetPageId("about_us");
    //        txtBox.Text = objManger.GetPageContent(PageId);
    //    }
    //}
    //protected void btnUpdate_Click(object sender, EventArgs e)
    //{
    //    ISylvainManager objManger = new SylvainManager();
    //    objManger.InsertPageContent(PageId, txtBox.Text, "jitin");
    //}


    /// <summary>
    /// page load operations
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bind the product grid
            BindProductGrid();
        }
    }
    /// <summary>
    ///  Function to add product..
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Contract objContract = new Contract();
            string filename = string.Empty;
           
            //Save file
            if (fileuploadImage.HasFile)
            {

                string[] str = new string[2];
                str = fileuploadImage.FileName.Split('.');
                filename = str[0] + Guid.NewGuid() + "." + str[1];
                string imagePath = Server.MapPath("~/Admin/AdminImages/");//ConfigurationManager.AppSettings["ItemImagePath"];
                fileuploadImage.SaveAs(imagePath +
                     filename);
            }
          
            objContract.ClientName = txtClientName.Text.Trim();
            objContract.Title = txtTitle.Text.Trim();
            objContract.Description = txtContractDescription.Text.Trim();
            objContract.Image = filename;
            objContract.DateCreated = System.DateTime.Now;

            // insert product
            objManger.AddContract(objContract);


            BindProductGrid();
            ResetControls();
            lblError.Text = "Submitted Successfully";

          
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }

    }

    /// <summary>
    /// button update operations
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {

          Contract objContract = new Contract();
            string filename=string.Empty;
            //Update Image
            if (fileuploadImage.HasFile)
            {

                string[] str = new string[2];
                str = fileuploadImage.FileName.Split('.');
                filename=str[0] + Guid.NewGuid() + "." + str[1];
                string imagePath = Server.MapPath("~/Admin/AdminImages/");//ConfigurationManager.AppSettings["ItemImagePath"];
                fileuploadImage.SaveAs(imagePath +
                     filename);
            }
            
            objContract.ClientName = txtClientName.Text.Trim();
            objContract.Id = int.Parse(hidContractId.Value);
            objContract.Title = txtTitle.Text.Trim();
            objContract.Description = txtContractDescription.Text.Trim();
            objContract.Image = filename;
            objContract.DateCreated = System.DateTime.Now;
            // update product
            objManger.EditContract(objContract);

           
            //bind product grid
            BindProductGrid();
            lblError.Text = "Updated Successfully";
            ResetControls();

            btnUpdate.Visible = false;
            btnSubmit.Visible = true;
            reqImageVal.Enabled = true;
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Function to bind the grid with products
    /// </summary>
    protected void BindProductGrid()
    {


        grdContracts.DataSource = objManger.GetAllContracts();
        grdContracts.DataBind();

    }
    /// <summary>
    /// Function to delete the product 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdContracts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                int contractID = Convert.ToInt32(e.CommandArgument);
               
                //Delete the product
                Contract objContract= objManger.DeleteContract(contractID);
                if (objContract != null && objContract.Image != null && objContract.Image != "")
                {
                    try
                    {
                        if (File.Exists(Server.MapPath("~/Admin/AdminImages/" + objContract.Image)))
                        {
                            File.Delete(Server.MapPath("~/Admin/AdminImages/" + objContract.Image));
                        }
                    }
                    catch { }
                }
                BindProductGrid();
            }
            if (e.CommandName == "Update")
            {
                hidContractId.Value = e.CommandArgument.ToString();
                btnSubmit.Visible = false;
                btnUpdate.Visible = true;
                reqImageVal.Enabled = false;
                lblError.Text = "";
            }
        }
        catch
        {
            lblError.Text = "Product is associated with Product types and can't be deleted.";
        }
    }
    /// <summary>
    /// Reset Controls
    /// </summary>
    private void ResetControls()
    {
        txtClientName.Text = "";
        txtContractDescription.Text = "";
        txtTitle.Text = "";
        reqImageVal.Enabled = true;
    }
    /// <summary>
    /// function to handle the delete command 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdContracts_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ResetControls();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdContracts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {

            GridViewRow row = (GridViewRow)grdContracts.Rows[e.RowIndex];
            txtClientName.Text = ((Label)row.FindControl("lblClientName")).Text;
            txtContractDescription.Text = ((Label)row.FindControl("lblDescription")).Text;
            txtTitle.Text = ((Label)row.FindControl("lblTitle")).Text;
            btnSubmit.Visible = false;
            btnUpdate.Visible = true;
            reqImageVal.Enabled = false;
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }

    }
    /// <summary>
    /// paging event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdContracts_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        grdContracts.PageIndex = e.NewPageIndex;
        BindProductGrid();
    }

}